<?php 
	error_reporting(0);
	session_start();
	if(empty($_SESSION)) {
		die("你当前未登录，请点击<a href='index.php'>登录</a>");
	}
?>
<!DOCTYPE HTML>
<html lang="">
<head>
	<meta charset="UTF-8">
	<title>个人中心 - 一书网</title>
	<link rel="stylesheet" type="text/css" href="style/user_zone.css">
</head>
<body>
	<div class="top-nav">
		<div class="nav-info">
			<div class="nav">
				<ul>
					<li><a href="user_zone.php?action=info">我的信息</a></li>
					<li><a href="user_zone.php?action=orders">我的购物车</a></li>
					<li><a href="user_zone.php?action=ship">我的订单</a></li>
				</ul>
			</div>
			<div class="info">
				<span><a href="index.php">返回首页</a>　　</span>
				<span>当前用户：<?php echo $_SESSION['user_name']; ?>　　</span>
				<span>登录时间：<?php echo date("Y-m-d",time()); ?></span>
			</div>
		</div>
	</div>
	<?php 
		$id = $_SESSION['user_id'];
		$user_name = $_SESSION['user_name'];
		$tel = $_SESSION['tel'];
		
		switch ($_GET['action']) {
			case "info":
				echo "<div class='user-info'>
						<h3>我的信息</h3>
						<hr />
						<table>
							<tr>
								<td class='th'>ID</td>
								<td class='th'>用户名</td>
								<td class='th'>手机号</td>
							</tr>
							<tr class='info-tr'>
								<td>{$id}</td>
								<td>{$user_name}</td>
								<td>{$tel}</td>
							</tr>
						</table>
					</div>";
				break;
			
			case "orders":
				// 1、导入数据库连接文件
				include 'link.php';
				
				// 2、生成表头信息
				echo "<div class='user-order'>
						<h3>我的购物车</h3>
						<hr />
						<table>
							<tr>
								<td class='th'>订单号</td>
								<td class='th'>书名</td>
								<td class='th'>单价</td>
								<td class='th'>订购数</td>
								<td class='th'>应付金额</td>
								<td class='th'>操作</td>
							</tr>";
				
				// 3、执行数据库查询
				$query = "select * from orders";
				$result = mysqli_query($link, $query);

				// 定位到第一条记录
				mysqli_data_seek($result, 0);
				
				// 4、循环取出记录,生成展示商品信息的表格
				if($result){
					while ($rows = mysqli_fetch_array($result)){
						echo "<tr class='order-tr'>
								<td>{$rows['order_no']}</td>
								<td>{$rows['book_name']}</td>
								<td>{$rows['price']}</td>
								<td>{$rows['order_quantity']}</td>
								<td>{$rows['amount']}</td>
								<td>
									<a href='user_zone.php?action=delete&id={$rows['order_no']}'>删除</a>
									 ｜ <a href='pay.php?id={$rows['order_no']}'>结账</a>	
								</td>
							</tr>";							
					}
				}
				echo "</table>
					</div>";
				break;

			case "ship":
				// 1、导入数据库连接文件
				include 'link.php';
				
				// 2、生成表头信息
				echo "<div class='user-ship'>
						<h3>我的订单</h3>
						<hr />
						<table>
							<tr>
								<td class='th'>配送单号</td>
								<td class='th'>书名</td>
								<td class='th'>订购数量</td>
								<td class='th'>实付金额</td>
								<td class='th'>配送地址</td>
								<td class='th'>快递公司</td>
								<td class='th'>配送状态</td>
							</tr>";
				
				// 3、执行数据库查询
				$query = "select * from ship";
				$result = mysqli_query($link, $query);

				// 定位到第一条记录
				mysqli_data_seek($result, 0);
				
				// 4、循环取出记录,生成展示订单信息的表格
				if($result){
					while ($rows = mysqli_fetch_array($result)){
						echo "<tr class='order-tr'>
								<td>{$rows['ship_no']}</td>
								<td>{$rows['book_name']}</td>
								<td>{$rows['order_quantity']}</td>
								<td>{$rows['amount']}</td>
								<td>{$rows['ship_addr']}</td>
								<td>{$rows['ship_company']}</td>
								<td>{$rows['ship_state']}</td>
							</tr>";							
					}
				}
				echo "</table>
					</div>";
				break;

			case "delete":
				// 1、获取对应的订单号
				$order_no = $_GET['id'];
				
				// 2、在数据库中读取对应订单号的相关信息
				// 3、导入数据库连接文件
				include 'link.php';
				
				// 4、查询相关信息并存入相应变量
				$query = "delete from orders where order_no={$order_no}";
				$result = mysqli_query($link, $query);
				if(!$result) {
					die("删除失败！请重试。");
				} else {
					header("Location:user_zone.php?action=orders");
				}
				
				
			default:
				echo "<div class='user-info'>
						<h3>我的信息</h3>
						<hr />
						<table>
							<tr>
								<td class='th'>ID</td>
								<td class='th'>用户名</td>
								<td class='th'>手机号</td>
							</tr>
							<tr class='info-tr'>
								<td>{$id}</td>
								<td>{$user_name}</td>
								<td>{$tel}</td>
							</tr>
						</table>
					</div>";
				break;
		}
	 ?>
</body>
</html>